using System;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Drawing;

[assembly: TagPrefix("BbqFramework.Web.UI.Controls", "bbq")]

[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.prototype.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.effects.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.lightbox.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.css.lightbox.css", "text/stylesheet", PerformSubstitution = true)]

[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.prototype.lite.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.moo.fx.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.litebox-1.0.js", "text/javascript", PerformSubstitution = true)]

[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.thickbox.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.thickbox_global.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.thickbox_jquery_compressed.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.css.thickbox.css", "text/stylesheet", PerformSubstitution = true)]

[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.js.lytebox.js", "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource("BbqFramework.Web.UI.Controls.css.lytebox.css", "text/stylesheet", PerformSubstitution = true)]


namespace BbqFramework.Web.UI.Controls
{
    /// <summary>
    /// All type of coolbox
    /// </summary>
    public enum CoolBoxTypes
    {
        /// <summary>
        /// Lightbox is a simple, unobtrusive script used to overlay images on the current page.
        /// </summary>
        LightBox,
        /// <summary>
        /// Similar as Lightbox but ThickBox can show html pages as well as images.
        /// </summary>
        ThickBox,
        /// <summary>
        /// The same as Lightbox but more light
        /// </summary>
        LiteBox,
        /// <summary>
        /// Another version of Lightbox but simpler and richer features.
        /// </summary>
        LyteBox,
    }

    /// <summary>
    /// This class is a wrapper for some cool javascript such as Lightbox, Thickbox with some expansion. 
    /// It's a snap to setup and works on all modern browsers.
    /// </summary>
    /// <remarks>You can refer to the homepage of original script at http://www.huddletogether.com/projects/lightbox2. </remarks>
    [DefaultProperty("Type")]
    [ToolboxData("<{0}:Coolbox runat=server></{0}:Coolbox>")]
    [ToolboxBitmap(typeof(CoolBox), "Coolbox.bmp")]
    [Designer(typeof(CoolBoxDesigner))]
    public class CoolBox : Control
    {
        #region Private Constants
        private const string LITEBOX_PROTOTYPE = "BbqFramework.Web.UI.Controls.js.prototype.lite.js";
        private const string LITEBOX_EFFECTS = "BbqFramework.Web.UI.Controls.js.moo.fx.js";
        private const string LITEBOX_LIGHTBOX = "BbqFramework.Web.UI.Controls.js.litebox-1.0.js";

        private const string LIGHTBOX_PROTOTYPE = "BbqFramework.Web.UI.Controls.js.prototype.js";
        private const string LIGHTBOX_EFFECTS = "BbqFramework.Web.UI.Controls.js.effects.js";
        private const string LIGHTBOX_LIGHTBOX = "BbqFramework.Web.UI.Controls.js.lightbox.js";
        private const string LIGHTBOX_CSS = "BbqFramework.Web.UI.Controls.css.lightbox.css";
        private const string LIGHTBOX_CHANGE_IMG_PATH = "Lightbox_ChangeImgPath";

        private const string THICKBOX_GLOBAL = "BbqFramework.Web.UI.Controls.js.thickbox_global.js";
        private const string THICKBOX_THICKBOX = "BbqFramework.Web.UI.Controls.js.thickbox.js";
        private const string THICKBOX_JQUERY_COMPRESSED = "BbqFramework.Web.UI.Controls.js.thickbox_jquery_compressed.js";
        private const string THICKBOX_CSS = "BbqFramework.Web.UI.Controls.css.thickbox.css";

        private const string PROP_TYPE = "Type";
        private const string PROP_CLOSE_IMG = "CloseImage";
        private const string PROP_LOADING_IMG = "LoadingImage";
        private const string PROP_NEXT_IMG = "NextImage";
        private const string PROP_PREV_IMG = "PreviousImage";
        private const string PROP_BLANK_IMG = "BlankImage";
        #endregion

        /// <summary>
        /// Gets or sets the type of coolbox.
        /// </summary>
        /// <value>A CoolBoxTypes value.</value>
        [Category("CoolBox")]
        [DefaultValue(CoolBoxTypes.LightBox)]
        [Description("Choose the type of CoolBox. Each type has one or some special effects.")]
        public CoolBoxTypes Type
        {
            get
            {
                object obj = ViewState[PROP_TYPE];
                return obj == null ? CoolBoxTypes.LightBox : (CoolBoxTypes)obj;
            }
            set
            {
                ViewState[PROP_TYPE] = value;
            }
        }

        /// <summary>
        /// Gets or sets the close image.
        /// </summary>
        /// <value>The close image.</value>
        [Category("CoolBox")]
        [DefaultValue("")]
        [Description("Gets or sets the close image.")]
        public string CloseImage
        {
            get
            {
                string obj = (string)ViewState[PROP_CLOSE_IMG];
                return obj ?? string.Empty;
            }
            set { ViewState[PROP_CLOSE_IMG] = value; }
        }

        /// <summary>
        /// Gets or sets the loading image.
        /// </summary>
        /// <value>The loading image.</value>
        [Category("CoolBox")]
        [DefaultValue("")]
        [Description("Gets or sets the loading image.")]
        public string LoadingImage
        {
            get
            {
                string obj = (string)ViewState[PROP_LOADING_IMG];
                return obj ?? string.Empty;
            }
            set { ViewState[PROP_LOADING_IMG] = value; }
        }

        /// <summary>
        /// Gets or sets the next image.
        /// </summary>
        /// <value>The next image.</value>
        [Category("CoolBox")]
        [DefaultValue("")]
        [Description("Gets or sets the next image.")]
        public string NextImage
        {
            get
            {
                string obj = (string)ViewState[PROP_NEXT_IMG];
                return obj ?? string.Empty;
            }
            set { ViewState[PROP_NEXT_IMG] = value; }
        }

        /// <summary>
        /// Gets or sets the previous image.
        /// </summary>
        /// <value>The previous image.</value>
        [Category("CoolBox")]
        [DefaultValue("")]
        [Description("Gets or sets the previous image.")]
        public string PreviousImage
        {
            get
            {
                string obj = (string)ViewState[PROP_PREV_IMG];
                return obj ?? string.Empty;
            }
            set { ViewState[PROP_PREV_IMG] = value; }
        }

        /// <summary>
        /// Gets or sets the blank image.
        /// </summary>
        /// <value>The blank image.</value>
        [Category("CoolBox")]
        [DefaultValue("")]
        [Description("Gets or sets the blank image.")]
        public string BlankImage
        {
            get
            {
                string obj = (string)ViewState[PROP_BLANK_IMG];
                return obj ?? string.Empty;
            }
            set { ViewState[PROP_BLANK_IMG] = value; }
        }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.DesignMode) return;
            string css = null;
            if (this.Type != CoolBoxTypes.ThickBox)   //Lightbox
            {
                //Register script file
                if (this.Type == CoolBoxTypes.LiteBox)
                {
                    if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(typeof(CoolBox), LITEBOX_PROTOTYPE))
                        Page.ClientScript.RegisterClientScriptInclude(typeof(CoolBox), LITEBOX_PROTOTYPE,
                            this.Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), LITEBOX_PROTOTYPE));
                    if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(typeof(CoolBox), LITEBOX_EFFECTS))
                        Page.ClientScript.RegisterClientScriptInclude(typeof(CoolBox), LITEBOX_EFFECTS,
                            this.Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), LITEBOX_EFFECTS));
                    if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(typeof(CoolBox), LITEBOX_LIGHTBOX))
                        Page.ClientScript.RegisterClientScriptInclude(typeof(CoolBox), LITEBOX_LIGHTBOX,
                            this.Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), LITEBOX_LIGHTBOX));
                }
                else    //LiteBox
                {
                    if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(typeof(CoolBox), LIGHTBOX_PROTOTYPE))
                        Page.ClientScript.RegisterClientScriptInclude(typeof(CoolBox), LIGHTBOX_PROTOTYPE,
                            this.Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), LIGHTBOX_PROTOTYPE));
                    if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(typeof(CoolBox), LIGHTBOX_EFFECTS))
                        Page.ClientScript.RegisterClientScriptInclude(typeof(CoolBox), LIGHTBOX_EFFECTS,
                            this.Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), LIGHTBOX_EFFECTS));
                    if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(typeof(CoolBox), LIGHTBOX_LIGHTBOX))
                        Page.ClientScript.RegisterClientScriptInclude(typeof(CoolBox), LIGHTBOX_LIGHTBOX,
                            this.Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), LIGHTBOX_LIGHTBOX));
                }
                if (!Page.ClientScript.IsStartupScriptRegistered(typeof(CoolBox), LIGHTBOX_CHANGE_IMG_PATH))
                {
                    StringBuilder sb = new StringBuilder();
                    if (this.CloseImage.Length > 0)
                    {
                        sb.Append("fileBottomNavCloseImage = '");
                        sb.Append( HttpUtility.HtmlEncode(this.ResolveUrl(this.CloseImage)));
                        sb.Append("';");
                    }
                    if (this.LoadingImage.Length > 0)
                    {
                        sb.Append( "fileLoadingImage = '");
                        sb.Append(HttpUtility.HtmlEncode(this.ResolveUrl(this.LoadingImage)));
                        sb.Append("';");
                    }
                    string script = sb.ToString();
                    sb = new StringBuilder();
                    sb.Append(@"<style type=""text/css"">
#prevLink, #nextLink{
	width: 49%;
	height: 100%;
	background: transparent url(");
                    sb.Append(HttpUtility.HtmlEncode(this.ResolveUrl(this.BlankImage)));
                    sb.Append(@") no-repeat; /* Trick IE into showing hover */
	display: block;
	}
#prevLink { left: 0; float: left;}
#nextLink { right: 0; float: right;}
#prevLink:hover, #prevLink:visited:hover { background: url(");
                    sb.Append(HttpUtility.HtmlEncode(this.ResolveUrl(PreviousImage)));
                    sb.Append(@") left 15% no-repeat; }
#nextLink:hover, #nextLink:visited:hover { background: url(");
                    sb.Append(HttpUtility.HtmlEncode(this.ResolveUrl(this.NextImage)));
                    sb.Append(@") right 15% no-repeat; }
</style>");
                    if (script.Length > 0)
                        Page.ClientScript.RegisterStartupScript(typeof(CoolBox), LIGHTBOX_CHANGE_IMG_PATH, script, true);
                    Page.Header.Controls.Add(new LiteralControl(sb.ToString()));
                }
                //Register CSS file
                css = string.Concat("<link rel='stylesheet' text='text/css' href='",
                    Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), LIGHTBOX_CSS), "' media='screen' />");
            }
            else    //Thickbox
            {
                if (!Page.ClientScript.IsClientScriptIncludeRegistered(typeof(CoolBox), THICKBOX_JQUERY_COMPRESSED))
                    Page.ClientScript.RegisterClientScriptInclude(typeof(CoolBox), THICKBOX_JQUERY_COMPRESSED,
                        Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), THICKBOX_JQUERY_COMPRESSED));
                if (!Page.ClientScript.IsClientScriptIncludeRegistered(typeof(CoolBox), THICKBOX_THICKBOX))
                    Page.ClientScript.RegisterClientScriptInclude(typeof(CoolBox), THICKBOX_THICKBOX,
                        Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), THICKBOX_THICKBOX));
                css = string.Concat("<link rel='stylesheet' text='text/css' href='",
                    Page.ClientScript.GetWebResourceUrl(typeof(CoolBox), THICKBOX_CSS), "' media='all' />");
            }
            LiteralControl linkCss = new LiteralControl(css);
            Page.Header.Controls.Add(linkCss);
        }

    }
}
